package cn.java78.jdbc;

import java.sql.*;

/**
 * @author oldliu
 * @since 1.0
 */
public class Test1 {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("驱动没有找到！");
            e.printStackTrace();
            System.exit(0);
        }
        //前面固定为mysql格式+ip+端口+库名+其他参数
        String url = "jdbc:mysql://localhost:3306/java78?useSSL=false&characterEncoding=UTF-8&serverTimezone=GMT%2b8";
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try {
            con = DriverManager.getConnection(url, "root", "root1234");
            System.out.println("具体类：" + con.getClass().getName());
            st = con.createStatement();
            rs = st.executeQuery("select * from s_subject order by id desc");
            while (rs.next()) {
                long id = rs.getLong("id");
                String subjectname = rs.getString("subjectname");
                int classhour = rs.getInt("classhour");
                long gradeid = rs.getLong("gradeid");
                System.out.printf("%d ,%s,%d,%d", id, subjectname, classhour, gradeid);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {

            try {
                if (rs != null)
                    rs.close();

            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {

                if (st != null)
                    st.close();

            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {

                if (con != null)
                    con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }
}
